import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ButtonAddPerson extends JButton
{
	private JTextField _txtFieldName, _txtFieldAge;
	private Store _store;

	public ButtonAddPerson(JTextField _txtFieldName, JTextField _txtFieldAge, Store _store)
	{
		super("Create Customer");
		this._store = _store;
		this.addActionListener(new Listener());
		this._txtFieldName = _txtFieldName;
		this._txtFieldAge = _txtFieldAge;
	}
	
	private class Listener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			try
			{
				int ageForPerson = Integer.parseInt(_txtFieldAge.getText().trim());
				String nameForPerson = _txtFieldName.getText();
				_store.addCustomer(nameForPerson, ageForPerson);
				JOptionPane.showMessageDialog(null, "The person - " + nameForPerson +" - was added to the store!", "Person Added", JOptionPane.INFORMATION_MESSAGE);
			}
			catch(Exception exc)
			{
				JOptionPane.showMessageDialog(null, "Wrong age input; no person was added.", "Error", JOptionPane.ERROR_MESSAGE);
			}

			
		}
	}
}
